package com.lxw.nonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxw.nonghe.entity.PolicyAttachment;
import com.lxw.nonghe.mapper.PolicyAttachmentMapper;
import com.lxw.nonghe.service.PolicyAttachmentService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 政策附件表 服务实现类
 * </p>
 *
 * @author lxw
 * @since 2025-05-14
 */
@Service
public class PolicyAttachmentServiceImpl
        extends ServiceImpl<PolicyAttachmentMapper, PolicyAttachment>
        implements PolicyAttachmentService {

    @Override
    public Page<PolicyAttachment> getAttachmentPage(
            int current, int size,
            Integer policyId, String attachmentName) {

        QueryWrapper<PolicyAttachment> wrapper = new QueryWrapper<>();

        // 政策ID精确查询
        if (policyId != null) {
            wrapper.eq("policy_id", policyId);
        }

        // 附件名称模糊查询
        if (StringUtils.isNotBlank(attachmentName)) {
            wrapper.like("attachment_name", attachmentName.trim());
        }

        // 排序，最新创建的在前面
        wrapper.orderByDesc("create_time");

        // 执行分页查询
        return this.page(new Page<>(current, size), wrapper);
    }
    // 条件查询实现
    @Override
    public Page<PolicyAttachment> queryAttachments(
            PolicyAttachment queryParam,
            int current, int size) {

        QueryWrapper<PolicyAttachment> wrapper = new QueryWrapper<>();

        // 附件ID精确查询
        if (queryParam.getAttachmentId() != null) {
            wrapper.eq("attachment_id", queryParam.getAttachmentId());
        }

        // 政策ID精确查询
        if (queryParam.getPolicyId() != null) {
            wrapper.eq("policy_id", queryParam.getPolicyId());
        }

        // 附件名称模糊查询
        if (StringUtils.isNotBlank(queryParam.getAttachmentName())) {
            wrapper.like("attachment_name", queryParam.getAttachmentName().trim());
        }

        // 附件路径模糊查询
        if (StringUtils.isNotBlank(queryParam.getAttachmentPath())) {
            wrapper.like("attachment_path", queryParam.getAttachmentPath().trim());
        }

        // 创建时间范围查询 - 大于等于
        if (queryParam.getCreateTime() != null) {
            wrapper.ge("create_time", queryParam.getCreateTime());
        }

        // 排序，最新创建的在前面
        wrapper.orderByDesc("create_time");

        // 执行分页查询
        return this.page(new Page<>(current, size), wrapper);
    }
}
